#include <my_head.h>
#define ERRLOG(msg)                                       \
    do                                                    \
    {                                                     \
        perror(msg);                                      \
        printf("%s-%s-%d", __FILE__, __func__, __LINE__); \
        exit(-1);                                         \
    } while (0)

typedef struct __MSG{
    char handle_code;
    char user_name[32];
    char text_box[256];
}msg_t;
void print_manu();
void print_manu2();
void registered(int sockfd);
int login(int sockfd,char *name);
void do_login_after(int sockfd,char *name);
void search(int sockfd,char *name);
void hostory(int sockfd,char *name);
int main(int argc, char const *argv[])
{
    if (argc != 3)
    {
        printf("Usage: %s <ip> <port>\n", argv[0]);
        exit(-1);
    }
    //调用数据库初始化函数，创建数据库和历史记录和用户的表
    //sqlite3 *proc_init()

    //创建socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        ERRLOG("socked error");
    }
    //创建结构体
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    socklen_t serveraddrlen = sizeof(serveraddr);

    if(-1 == connect(sockfd,(struct sockaddr *)&serveraddr,serveraddrlen)){
        ERRLOG("connect error");
    }
    int acceptfd = 0;
    //循环接收
    int choose = 0;
    char u_name[32] = {0};  //用来保存登录后的用户名
    memset(u_name,0,32);
//LOOP:
    while (1)
    {
        print_manu();
        scanf("%d",&choose);
       
        switch (choose)
        {
        case 1:
            registered(sockfd);
            break;
        case 2:
            if(0==login(sockfd,u_name)){
                do_login_after(sockfd,u_name);
            }
            break;
        case 3:
            break;
        }
        if(choose == 3){
            printf("退出系统\n");
            break;
        }
        
    }

    close(sockfd);
    return 0;
}
void print_manu(){
    printf("*****************************\n");
    printf("****1.注册 2.登录 3.退出 ***\n");
    printf("*****************************\n");
    printf("input your choose>>");
}

void print_manu2(){
    printf("********************************\n");
    printf("****1.查询 2.历史记录 3.退出 ***\n");
    printf("********************************\n");
    printf("input your choose>>");
}
void do_login_after(int sockfd,char *name){
    int n = 0;
    print_manu2();
    scanf("%d",&n);
    while(1){
        switch(n)
        {
        case 1:
            search(sockfd,name);
            break;
        case 2:
            hostory(sockfd,name);
            break;
        case 3:
            break;
        }
        if(n==3){
            break;
            //goto LOOP;
        }
    }
    
}
//注册
void registered(int sockfd){
    msg_t msg;
    memset(&msg,0,sizeof(msg));
    char name[32] = {0};
    char pass[32] = {0};
    printf("请输入用户名 (name TEXT):");
    //printf("测试0001111110000");
    scanf("%s",name);
    printf("请输入注册密码 (pass TEXT):");
    scanf("%s",pass);
    //printf("测试000000000000");
    //封装msg
    msg.handle_code = 'R';
    strcpy(msg.user_name,name);
    strcpy(msg.text_box,pass);
    
    //发送 密码用户名
    if(-1 == send(sockfd,&msg,sizeof(msg),0)){
        ERRLOG("send error");
    }
    //printf("测试11111111111111111");
    memset(&msg,0,sizeof(msg));
    //接收返回值
    if(-1 == recv(sockfd,&msg,sizeof(msg),0)){
        ERRLOG("recv error");
    }
    printf("%s\n",msg.text_box);
    
}
//登录
int login(int sockfd,char *name){
    msg_t msg;
    memset(&msg,0,sizeof(msg));
    msg.handle_code = 'L';
    printf("请输入用户名和登录密码 (name TEXT, pass TEXT):");
    scanf("%s%s", msg.user_name, msg.text_box);
    //发送 密码用户名
    if(-1 == send(sockfd,&msg,sizeof(msg),0)){
        ERRLOG("send error");
    }

    memset(&msg.text_box,0,sizeof(msg.text_box));
    //接收返回值
    if(-1 == recv(sockfd,&msg,sizeof(msg),0)){
        ERRLOG("recv error");
    }
    if(strcmp(msg.text_box,"OK")==0){
        printf("%s\n",msg.text_box);
        strcpy(name,msg.user_name);
        return 0;
    }else{
        printf("%s\n",msg.text_box);
        return -1;
    }
    
}

//查询,需要把登录后的用户名传过来
void search(int sockfd,char *name){
    //printf("yangfs---------------------\n");
    msg_t msg;
    char word[32] = {0};
    memset(&msg,0,sizeof(msg));
    msg.handle_code = 'S';
    //msg.user_name = name;  //字符数组不能直接赋值
    strcpy(msg.user_name,name);
    printf("请输入需要查询的单词:");
    scanf("%s",word);
    //printf("*****************\n");
     //判断以下单词，如果输入的是over的话，说明我不想查单词了，单词不用发送了，返回上一层。
    if(strcmp(word,"end")==0){
        do_login_after(sockfd,name);
        exit(-1);        //结束后面的进程。
    }
    strcpy(msg.text_box,word);
    
    //发送 msg，操作码，用户，单词
    //printf("测试0001111110000\n");
    if(-1 == send(sockfd,&msg,sizeof(msg),0)){
        ERRLOG("send error");
    }
    //printf("测试11111111111111\n");
   
    memset(&msg,0,sizeof(msg));
    //接收返回值
    if(-1 == recv(sockfd,&msg,sizeof(msg),0)){
        ERRLOG("recv error");
    }
    if(strcmp(msg.text_box,"not found")==0){
        printf("查询的单词不存在\n");
        
    }else{
        printf("[%s]:%s\n",word,msg.text_box);
       
    }
}
//历史记录
void hostory(int sockfd,char *name){
    int temp_flag = 0;    //打印“你的查询历史记录如下”标志位
    msg_t msg;
    int bytes = 0;
    memset(&msg,0,sizeof(msg));
    msg.handle_code = 'H';
    strcpy(msg.user_name,name);
    if(-1 == send(sockfd,&msg,sizeof(msg),0)){
       ERRLOG("send error");
    }
    //需要循环接收
    
    while(1){
        if(-1 == (bytes = recv(sockfd,&msg,sizeof(msg),0))){
            ERRLOG("recv error");
        }else if(bytes == 0){
            printf("没有历史记录\n");
        }else{
            if(strcmp(msg.text_box,"over")==0){
                //printf("lzq--------");
                break;
            }
            if(temp_flag == 0){
                printf("你的查询历史记录如下:\n");
                temp_flag = 1;
            }
            printf("%10s",msg.user_name);
            printf("---");
            printf("%10s",msg.text_box);
            printf("\n"); 
        }
        memset(msg.text_box,0,sizeof(msg.text_box));
    }
    temp_flag = 0;   //标志位置0
    //printf("验证查询完是否能退出循环\n");
    do_login_after(sockfd,name);
    exit(-1);        //结束后面的进程。
}

